<?php
// get current active menu item
function dt_current_menu_item( $item = null ) {
    static $data = null;
    
    if( $item ) {
        
        switch( $item->post_type ) {
            case 'nav_menu_item':
            
                $data = array(
                    'title'     => $item->title,
                    'link'      => $item->url
                );
            
                break;
            default:
                $data = array(
                    'title'     => $item->post_title,
                    'link'      => get_permalink($item->ID)
                );
        }

    }
    
    return $data;
}

// walker nav_menu
class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
    private $dt_options = array();
    private $dt_menu_parents = array();
    private $dt_last_elem = 1;
    private $dt_count = 1;

    function __construct( $options = array() ) {
        if( method_exists('Walker_Nav_Menu','__construct') ){
            parent::__construct();
        }
        
        if( is_array($options) ){
            $this->dt_options = $options;
        }
        
        $theme_location = isset($this->dt_options['theme_location'])?$this->dt_options['theme_location']:'';
        if ( $theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $theme_location ] ) ) {
            $menu = wp_get_nav_menu_object( $locations[ $theme_location ] );
            if( $menu ) {
                $menu_items = wp_get_nav_menu_items($menu->term_id);
                $prev = 0;
                foreach( $menu_items as $item ){
                    // nonclicable parent menu items
                    if( $prev != $item->menu_item_parent && $item->menu_item_parent ){
                        $this->dt_menu_parents[] = $item->menu_item_parent;
                        $prev = $item->menu_item_parent;
                    }
                    // last menu item
                    if( !$item->menu_item_parent ){
                        $this->dt_last_elem = $item->ID;
                    }
                }
                $this->dt_menu_parents = array_unique( $this->dt_menu_parents );
            }
        }
    }
    
    function start_lvl(&$output, $depth) {
            $output .= "\n<div style=\"visibility: hidden; display: block;\"><ul>\n";
    }
    
    function end_lvl(&$output, $depth) {
            $output .= "</ul><i></i></div>\n";
    }
    
    function start_el(&$output, $item, $depth, $args) {
            global $wp_query;
            $class_names = $value = '';
            $classes = empty( $item->classes ) ? array() : (array) $item->classes;
            $classes[] = 'menu-item-' . $item->ID;

/*            // last element
            if( $item->ID == $this->dt_last_elem ){
                $classes[] = 'last';
            }
*/            
            // current element
            if(
                in_array( 'current-menu-item',  $classes) ||
                in_array( 'current-menu-parent',  $classes) ||
                in_array( 'current-menu-ancestor',  $classes)
            ){
                $classes[] = 'act';
                dt_current_menu_item($item);
            }
           
            // cute little home in begining
            if( 1 == $this->dt_count ) {
                $output .= '<li class="home"><a href="'. home_url(). '" title="Home"></a></li>';
            }
            $this->dt_count++;
            
            $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
            $class_names = ' class="' . esc_attr( $class_names ) . '"';
            $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
            $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
            $output .= '<li' . $id . $value . $class_names .'>';
            
            $attributes  = ! empty( $item->attr_title )	? ' title="'  . esc_attr( $item->attr_title	) .'"' : '';
            $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
            $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
            $attributes .= ! empty( $item->url )   		? ' href="'   . esc_attr( $item->url   		) .'"' : '';

            // nonclicable parent menu items
            if( !$args->parent_clicable && in_array($item->ID, $this->dt_menu_parents) ){
                $attributes .= ' onclick="JavaScript: return false;"';
                $attributes .= ' style="cursor: default;"';
            }
            
            $item_output = isset($args->before)?$args->before:$args['before'];
            
            $item_output .= '<a'. $attributes .'>';
            $item_output .= (isset($args->link_before)?$args->link_before:'') . apply_filters( 'the_title', $item->title, $item->ID ) . (isset($args->link_after)?$args->link_after:'');
            $item_output .= '</a>';
            
            $item_output .= isset($args->after)?$args->after:'';
            
            $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}
// end walker nav_menu

// walker wp_list_pages
class Custom_Walker_Page extends Walker_Page {
    private $dt_menu_parents = array();
    private $dt_last_elem = 1;
    private $dt_count = 1;
    
    function __construct( $options = array() ) {
        if( method_exists('Walker_Page','__construct') ){
            parent::__construct();
        }
    
        $pages = get_pages('sort_column=menu_order,post_title');
        if( $pages ){
            $this->dt_pages = count($pages);
            $prev = 0;
            foreach( $pages as $page ){
                // nonclicable parent menu items
                if( $prev != $page->post_parent && $page->post_parent){
                    $this->dt_menu_parents[] = $page->post_parent;
                    $prev = $page->post_parent;
                }
                // last menu item
                if( !$page->post_parent ){
                    $this->dt_last_elem = $page->ID;
                }
            }
            $this->dt_menu_parents = array_unique( $this->dt_menu_parents );
        }
    }
    
    function start_lvl(&$output, $depth) {
            $output .= "\n<div style=\"visibility: hidden; display: block;\"><ul>\n";
    }
    
    function start_el(&$output, $page, $depth, $args, $current_page) {
            extract($args, EXTR_SKIP);
            $css_class = array('page_item', 'page-item-'.$page->ID);
            if ( !empty($current_page) ) {
                    $_current_page = get_page( $current_page );
                    _get_post_ancestors($_current_page);
                    if ( isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors) ){
                            $css_class[] = 'current_page_ancestor';
                            $css_class[] = 'act';
                            
                    }
                    if ( $page->ID == $current_page ){
                            $css_class[] = 'current_page_item';
                            $css_class[] = 'act';
                            
                    }elseif ( $_current_page && $page->ID == $_current_page->post_parent ){
                            $css_class[] = 'current_page_parent';
                            
                    }
            } elseif ( $page->ID == get_option('page_for_posts') ) {
                    $css_class[] = 'current_page_parent';
                    //$css_class[] = 'act';
                    
                    dt_current_menu_item( $page );
            }
            
            // cute little home in begining
            if( 1 == $this->dt_count ) {
                $output .= '<li class="home"><a href="'. home_url(). '" title="Home"></a></li>';
            }
            
            // last menu item
/*				if( $page->ID == $this->dt_last_elem ){
                $css_class[] = 'last';
            }
*/		    
            $this->prev_depth = $depth; 
            $attr = '';

            // nonclicable parent menu items
            if( in_array( $page->ID, $this->dt_menu_parents ) && !$args['parent_clicable']){
                $attr = ' onclick="JavaScript: return false;"';
                $attr .= ' style="cursor: default;"';
            }
            
            $css_class = implode(' ', apply_filters('page_css_class', $css_class, $page));
            $output .= '<li class="' . $css_class . '"><a' .$attr. ' href="' . get_permalink($page->ID) . '" title="' . esc_attr( wp_strip_all_tags( apply_filters( 'the_title', $page->post_title, $page->ID ) ) ) . '">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . '</a>';
            if ( !empty($show_date) ) {
                    if ( 'modified' == $show_date )
                            $time = $page->post_modified;
                    else
                            $time = $page->post_date;
                    $output .= " " . mysql2date($date_format, $time);
            }
            
            $this->dt_count++;
    }
    
    function end_lvl(&$output, $depth) {
            $output .= "</ul><i></i></div>\n";
    }
}
// end walker wp_list_pages

// function to fallof from wp_nav_menu
function dt_page_menu( $args = array() ) {
        $defaults = array(
            'sort_column'       => 'menu_order, post_title',
            'container_class'   => 'nav-bg',
            'menu_id'           => 'nav',
            'echo'              => false,
            'link_before'       => '',
            'link_after'        => ''
        );
        $args = wp_parse_args( $args, $defaults );
        $args = apply_filters( 'wp_page_menu_args', $args );
        $menu = '';
        $list_args = $args;
                    
        $list_args['echo'] = false;
        $list_args['title_li'] = '';
        $list_args['walker'] = new Custom_Walker_Page();
        $menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) );

        if ( isset( $menu ) )
            $menu = sprintf(
                $args['items_wrap'],
                $args['menu_id'],
                $args['menu_class'],
                $menu
            );
//            $menu = '<ul' . ( isset($args['menu_id'])?' id="'.esc_attr($args['menu_id']).'"':'' ) . '>' . $menu . '</ul>';

        if ( isset( $container ) )
            $menu = '<div class="' . esc_attr($args['container_class']) . '">' . $menu . "</div>\n";

        $menu = apply_filters( 'wp_page_menu', $menu, $args );
        if ( $args['echo'] ) {
            echo $menu;
        }else {
            return $menu;
        }
}

//-----------------------------testinggg-------------------------------------//

// walker nav_menu
class TEST_Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
    private $dt_options = array();
    private $dt_menu_parents = array();
    private $dt_last_elem = 1;
    private $dt_count = 1;

    function __construct( $options = array() ) {
        if( method_exists('Walker_Nav_Menu','__construct') ){
            parent::__construct();
        }
        
        if( is_array($options) ){
            $this->dt_options = $options;
        }
        
        $theme_location = isset($this->dt_options['theme_location'])?$this->dt_options['theme_location']:'';
        if ( $theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $theme_location ] ) ) {
            $menu = wp_get_nav_menu_object( $locations[ $theme_location ] );
            if( $menu ) {
                $menu_items = wp_get_nav_menu_items($menu->term_id);
                $prev = 0;
                foreach( $menu_items as $item ){
                    // nonclicable parent menu items
                    if( $prev != $item->menu_item_parent && $item->menu_item_parent ){
                        $this->dt_menu_parents[] = $item->menu_item_parent;
                        $prev = $item->menu_item_parent;
                    }
                    // last menu item
                    if( !$item->menu_item_parent ){
                        $this->dt_last_elem = $item->ID;
                    }
                }
                $this->dt_menu_parents = array_unique( $this->dt_menu_parents );
            }
        }
    }
    
    function start_lvl(&$output, $depth) {
        $output .= $this->dt_options['start_submenu'];
//            $output .= "\n<div style=\"visibility: hidden; display: block;\"><ul>\n";
    }
    
    function end_lvl(&$output, $depth) {
        $output .= $this->dt_options['end_submenu'];
//            $output .= "</ul><i></i></div>\n";
    }
    
    function start_el(&$output, $item, $depth, $args) {
            global $wp_query;
            $class_names = $value = '';
            $classes = empty( $item->classes ) ? array() : (array) $item->classes;
            $classes[] = 'menu-item-' . $item->ID;
            
            // current element
            if(
                in_array( 'current-menu-item',  $classes) ||
                in_array( 'current-menu-parent',  $classes) ||
                in_array( 'current-menu-ancestor',  $classes)
            ){
                $classes[] = 'act';
                dt_current_menu_item($item);
            }
           
            // cute little home in begining
            if( 1 == $this->dt_count ) {
                $output .= '<li class="home"><a href="'. home_url(). '" title="Home"></a></li>';
            }
            $this->dt_count++;
            
            $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
            $class_names = ' class="' . esc_attr( $class_names ) . '"';
            $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
            $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
            $output .= '<li' . $id . $value . $class_names .'>';
            
            $attributes  = ! empty( $item->attr_title )	? ' title="'  . esc_attr( $item->attr_title	) .'"' : '';
            $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
            $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
            $attributes .= ! empty( $item->url )   		? ' href="'   . esc_attr( $item->url   		) .'"' : '';

            // nonclicable parent menu items
            if( !$args->parent_clicable && in_array($item->ID, $this->dt_menu_parents) ){
                $attributes .= ' onclick="JavaScript: return false;"';
                $attributes .= ' style="cursor: default;"';
            }
            
            $item_output = isset($args->before)?$args->before:$args['before'];
            
            $item_output .= '<a'. $attributes .'>';
            $item_output .= (isset($args->link_before)?$args->link_before:'') . apply_filters( 'the_title', $item->title, $item->ID ) . (isset($args->link_after)?$args->link_after:'');
            $item_output .= '</a>';
            
            $item_output .= isset($args->after)?$args->after:'';
            
            $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
    
    function end_el(&$output, $item, $depth, $args) {
        $output .= "</li>\n";
    }
}
// end walker nav_menu

// walker wp_list_pages
class TEST_Custom_Walker_Page extends Walker_Page {
    private $dt_menu_parents = array();
    private $dt_last_elem = 1;
    private $dt_count = 1;
    
    function __construct( $options = array() ) {
        if( method_exists('Walker_Page','__construct') ){
            parent::__construct();
        }
    
        $pages = get_pages('sort_column=menu_order,post_title');
        if( $pages ){
            $this->dt_pages = count($pages);
            $prev = 0;
            foreach( $pages as $page ){
                // nonclicable parent menu items
                if( $prev != $page->post_parent && $page->post_parent){
                    $this->dt_menu_parents[] = $page->post_parent;
                    $prev = $page->post_parent;
                }
                // last menu item
                if( !$page->post_parent ){
                    $this->dt_last_elem = $page->ID;
                }
            }
            $this->dt_menu_parents = array_unique( $this->dt_menu_parents );
        }
    }
    
    function start_lvl(&$output, $depth, $args) {
        $output .= $args['dt_submenu_wrap_start'];
    }
    
    function start_el(&$output, $page, $depth, $args, $current_page) {
            extract($args, EXTR_SKIP);
            
            $css_class = array('page_item', 'page-item-'.$page->ID);
            if ( !empty($current_page) ) {
                    $_current_page = get_page( $current_page );
                    _get_post_ancestors($_current_page);
                    if ( isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors) ){
                            $css_class[] = 'current_page_ancestor';
                            $css_class[] = 'act';
                            
                    }
                    if ( $page->ID == $current_page ){
                            $css_class[] = 'current_page_item';
                            $css_class[] = 'act';
                            
                    }elseif ( $_current_page && $page->ID == $_current_page->post_parent ){
                            $css_class[] = 'current_page_parent';
                            
                    }
            } elseif ( $page->ID == get_option('page_for_posts') ) {
                    $css_class[] = 'current_page_parent';
                    //$css_class[] = 'act';
                    
                    dt_current_menu_item( $page );
            }
            
            // cute little home in begining
            if( 1 == $this->dt_count ) {                
                $dt_home_item = str_replace(
                    array(
                        '%ITEM_HREF%',
                        '%ITEM_TITLE%',
                        '%ITEM_CLASS%'
                    ),
                    array(
                        home_url(),
                        '',
                        'home'
                    ),
                    $args['dt_item_wrap_start'] . $args['dt_item_wrap_end']
                );
               
                $output .= $dt_home_item;
            }
             
            $attr = '';

            // nonclicable parent menu items
            if( in_array( $page->ID, $this->dt_menu_parents ) && !$args['parent_clicable']){
                $attr = ' onclick="JavaScript: return false;"';
                $attr .= ' style="cursor: default;';
            }
            
            $css_class = implode(' ', apply_filters('page_css_class', $css_class, $page));
            
            $output .= str_replace(
                array(
                    '%ITEM_HREF%',
                    '%ITEM_TITLE%',
                    '%ESC_ITEM_TITLE%',
                    '%ITEM_CLASS%'
                ),
                array(
                    get_permalink($page->ID) . '"' . $attr, //href
                    $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after, //item_title
                    esc_attr( wp_strip_all_tags( apply_filters( 'the_title', $page->post_title, $page->ID ) ) ), //a_item_title
                    $css_class //item_class
                ),
                $args['dt_item_wrap_start']
            );
                        
            if ( !empty($show_date) ) {
                    if ( 'modified' == $show_date )
                            $time = $page->post_modified;
                    else
                            $time = $page->post_date;
                    $output .= " " . mysql2date($date_format, $time);
            }
            
            $this->dt_count++;
    }
    
    function end_lvl(&$output, $depth, $args) {
        $output .= $args['dt_submenu_wrap_end'];
    }
    
    function end_el(&$output, $page, $depth, $args) {
        $output .= $args['dt_item_wrap_end'];
    }
}
// end walker wp_list_pages